﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace netcore_comm.SqlSugar

{
    public partial interface ISqlSugarRepository<T, D> where T : class, new() where D : DataBase
    {
        [Obsolete("不能在服务层获取数据库链接")]
        SqlSugarClient GetNewClient(bool useMainDb = false);

        #region
        //  获取模型对象
        T GetModel(SqlSugarClient db, dynamic id);
        T GetModel(SqlSugarClient db, Expression<Func<T, bool>> expression);

        //  获取所有
        List<T> GetList(SqlSugarClient db);
        List<T> GetList(SqlSugarClient db, string where);
        List<T> GetList(SqlSugarClient db, string where, SugarParameter sugarParameter = null);
        List<T> GetList(SqlSugarClient db, Expression<Func<T, bool>> expression);

        //   获取分页列表
        List<T> GetPageList(SqlSugarClient db, int pageIndex, int pageSize);
        List<T> GetPageList(SqlSugarClient db, int pageIndex, int pageSize, string where);
        List<T> GetPageList(SqlSugarClient db, int pageIndex, int pageSize, string where, SugarParameter sugarParameter = null);
        List<T> GetPageList(SqlSugarClient db, int pageIndex, int pageSize, Expression<Func<T, bool>> expression);

        //  增加
        bool Insert(SqlSugarClient db, T obj);

        //  根据主键删除
        bool Delete(SqlSugarClient db, T obj);
        bool Delete(SqlSugarClient db, Expression<Func<T, bool>> expression);

        //  根据主键批量删除
        bool DeleteList(SqlSugarClient db, List<dynamic> ids);
        bool DeleteList(SqlSugarClient db, string ids);

        //  更新
        bool Update(SqlSugarClient db, T obj);

        //  判断主键是否存在
        bool Exists(SqlSugarClient db, dynamic id);
        #endregion


        #region 异步方法
        //  获取模型对象
        Task<T> GetModelAsync(SqlSugarClient db, dynamic id);
        Task<T> GetModelAsync(SqlSugarClient db, Expression<Func<T, bool>> expression);
        Task<T> GetModelAsync(SqlSugarClient db, Expression<Func<T, bool>> expression, Expression<Func<T, object>> orderByExpression, OrderByType orderByType = OrderByType.Asc);

        //  获取所有
        Task<List<T>> GetListAsync(SqlSugarClient db);
        Task<List<T>> GetListAsync(SqlSugarClient db, string where);
        Task<List<T>> GetListAsync(SqlSugarClient db, string where, SugarParameter sugarParameter = null);
        Task<List<T>> GetListAsync(SqlSugarClient db, Expression<Func<T, bool>> expression);
        Task<List<T>> GetListAsync(SqlSugarClient db, Expression<Func<T, bool>> expression, Expression<Func<T, object>> orderByExpression, OrderByType orderByType = OrderByType.Asc);

        //   获取分页列表
        Task<List<T>> GetPageListAsync(SqlSugarClient db, int pageIndex, int pageSize);
        Task<List<T>> GetPageListAsync(SqlSugarClient db, int pageIndex, int pageSize, string where);
        Task<List<T>> GetPageListAsync(SqlSugarClient db, int pageIndex, int pageSize, string where, SugarParameter sugarParameter = null);
        Task<List<T>> GetPageListAsync(SqlSugarClient db, int pageIndex, int pageSize, Expression<Func<T, bool>> expression);

        //  增加
        Task<bool> InsertAsync(SqlSugarClient db, T obj);
        Task<T> SaveableAsync(SqlSugarClient db, T obj);

        //  根据主键删除
        Task<bool> DeleteAsync(SqlSugarClient db, T obj);
        Task<bool> DeleteAsync(SqlSugarClient db, Expression<Func<T, bool>> expression);

        //  根据主键批量删除
        Task<bool> DeleteListAsync(SqlSugarClient db, List<dynamic> ids);
        Task<bool> DeleteListAsync(SqlSugarClient db, string ids);

        //  更新
        Task<bool> UpdateAsync(SqlSugarClient db, T obj);

        //  判断主键是否存在
        Task<bool> ExistsAsync(SqlSugarClient db, dynamic id);

        #endregion
    }
}
